<?php
/**
 * Created by PhpStorm.
 * User: Apache
 * Date: 2019/12/5
 * Time: 9:54
 */

namespace app\api\controller\common;
use think\Model;

class ModelBase extends Model
{
    public $name;

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 插入数据
     * @param $data
     * @return bool|int|string
     */
    public function insertData($data)
    {
        return $this->insertGetId($data);
    }

    /**
     * 编辑数据
     * @param $data
     * @param $where
     * @return Project
     */
    public function updateBywhere($data,$where){
        $r = $this->where($where)->update($data);
        return $r;
    }

    /**
     * 获取总数
     * @param array $where
     * @return int|string
     * @throws \think\Exception
     */
    public function countWhere($where = []){

        $r = $this->alias('a')->where($where)->count();
        return $r;
    }

    /**
     * 假删
     * @param array $where
     * @return Project
     */
    public function deleteWhere($where = []){
        $r = $this->where($where)->update(['is_delete'=>1]);
        return $r;
    }

    /**
     * 获取详情
     * @param array $where
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function findWhere($where = []){
        $r = $this->where($where)->order('id desc')->find();
        return $r;
    }

    /**
     * 分页列表
     * @param array $where
     * @param string $order
     * @param int $current
     * @param int $pagesize
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function listWherePage($where = [],$order = 'id desc',$current = 0,$pagesize = 10){
        $r = $this->where($where)->order($order)->limit($current.','.$pagesize)->select();
        return $r;
    }
}